package ui;


import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;

import javax.swing.ImageIcon;
import javax.swing.JFrame;

/**
 * Creates the window which everything is shown upon.
 */

public class Window extends JFrame{

	public static final int width = 800; //Default width
	public static final int height = 800; //Default height
	private static Window window;
	
	/**
	 * Constructor of Window
	 */
	private Window(){
		setTitle("nebu");
		setSize(width, height);
		setPreferredSize(new Dimension(width, height));
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
	
	/**
	 * Sets the cursor to the given image.
	 * @param image is the image to be set as cursor.
	 */
	public void setCursor(ImageIcon image){
		this.setCursor(Toolkit.getDefaultToolkit().createCustomCursor(createCursorImage(image), new Point(0,0), "Custom Cursor"));
	}
	
	/**
	 * Sets the cursor to the given image.
	 * @param image is the image to be set as cursor.
	 */
	public void setCursor(Image image){
		this.setCursor(Toolkit.getDefaultToolkit().createCustomCursor(image, new Point(0,0), "Custom Cursor"));
	}
	/**
	 * Resets cursor to default value.
	 */
	public void resetCursor(){
		this.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
	}
	
	/**
	 * Creates a cursor image.
	 * @param image is the image which is to become a cursor.
	 * @return the cursor image as an BufferedImage.
	 */
	private BufferedImage createCursorImage(ImageIcon image){
	    BufferedImage buff = new BufferedImage(50, 50, BufferedImage.TYPE_INT_ARGB);
	    Graphics g = buff.createGraphics();
	    g.setColor(Color.WHITE);
	    g.fillRect(0, 0, 40, 50);
	    g.drawImage(image.getImage(), buff.getWidth()/2-image.getIconWidth()/2, buff.getHeight()/2-image.getIconHeight()/2, image.getIconWidth(), image.getIconHeight(), null);
	    g.dispose();
	    return buff;
	}
	
	/**
	 * Create Singelton instance of Window.
	 * @return the sole existing version of Window.
	 */
	public static Window getInstance(){
		if(window==null){
			window = new Window();
		}
		return window;
	}

}
